package com.smartcruise.util;

import android.text.TextUtils;

import com.smartcruise.MyApplication;

import java.io.File;
import java.math.BigDecimal;

/**
 * Created by FengChaoQun
 * on 2018/1/24 0024
 */

public class FileUtils {

    private static String RECORD_DATA = "recordData";
    private static String GLIDE = "imageCache";
    private static String AUDIO = RECORD_DATA + File.separator + "audio";
    private static String VIDeO = RECORD_DATA + File.separator + "video";
    private static String IMAGE = RECORD_DATA + File.separator + "image";
    private static String IMAGE_THUM = RECORD_DATA + File.separator + "imageThum";
    private static String IMAGE_TEMPLE = RECORD_DATA + File.separator + "imageTemple";
    public static String THUMBNAIL_SUFFIX = "_thumbnail";
    public static String MAP = "map";

    /*
  **describe:存储单位
  */
    public static int KB = 1024;
    public static int MB = 1024 * KB;
    public static int GB = 1024 * MB;


    /**
     * description:获取外部存储空间
     */

    public static String getExternalPath() {
        File file = MyApplication.getInstance().getExternalFilesDir(null);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return file.toString();
            } else {
                return null;
            }
        }
        return file.toString();
    }

    /**
     * description:获取Glide缓存目录
     */

    public static String getGlideCache() {
        File file = new File(getExternalPath(), GLIDE);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.toString();
    }

    /**
     * description:获取Map目录
     */

    public static String getMapPath() {
        File file = new File(getExternalPath(), MAP);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.toString();
    }

    /**
     * description:获取语音的目录路径
     */

    public static String getAudioPath() {
        File file = new File(getExternalPath(), AUDIO);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return file.getAbsolutePath();
            } else {
                return null;
            }
        }
        return file.getAbsolutePath();
    }

    /**
     * description:获取图片的目录路径
     */

    public static String getImagePath() {
        File file = new File(getExternalPath(), IMAGE);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return file.getAbsolutePath();
            } else {
                return null;
            }
        }
        return file.getAbsolutePath();
    }

    /**
     * description:获取临时图片的目录路径
     */

    public static String getImageTemplePath() {
        File file = new File(getExternalPath(), IMAGE_TEMPLE);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return file.getAbsolutePath();
            } else {
                return null;
            }
        }
        return file.getAbsolutePath();
    }

    /**
     * description:获取缩略图的目录路径
     */

    public static String getImageThumPath() {
        File file = new File(getExternalPath(), IMAGE_THUM);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return file.getAbsolutePath();
            } else {
                return null;
            }
        }
        return file.getAbsolutePath();
    }

    /**
     * description:获取视频的目录路径
     */

    public static String getVideoPath() {
        File file = new File(getExternalPath(), VIDeO);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return file.getAbsolutePath();
            } else {
                return null;
            }
        }
        return file.getAbsolutePath();
    }

    /**
     * description:获取一个新的语音路径
     */

    public static String getNewAudioPath() {
        File file = new File(getAudioPath(), System.currentTimeMillis() + ".mp3");
        return file.getAbsolutePath();
    }

    /**
     * description:获取一个新的图片路径
     */

    public static String getNewImagePath() {
        File file = new File(getImagePath(), System.currentTimeMillis() + ".jpg");
        return file.getAbsolutePath();
    }

    /**
     * description:获取一个新的视频路径
     */

    public static String getNewVideoPath() {
        File file = new File(getVideoPath(), System.currentTimeMillis() + ".mp4");
        return file.getAbsolutePath();
    }

    public static String getFileSimpleName(String path) {
        if (path != null) {
            String[] split = path.split(File.separator);
            return split[split.length - 1];
        }
        return null;
    }

    public static String getFileThumName(String path) {
        String name = getFileSimpleName(path);
        if (name != null) {
            String[] names = name.split("\\.");
            if (names.length == 2) {
                return names[0] + THUMBNAIL_SUFFIX + "." + names[1];
            }
        }
        return null;
    }

    public static String getVideoThumName(String path) {
        String name = getFileSimpleName(path);
        if (name != null) {
            String[] names = name.split("\\.");
            if (names.length == 2) {
                return names[0] + THUMBNAIL_SUFFIX + ".jpg";
            }
        }
        return null;
    }

    public static String getFileThumPath(String path) {
        String thumbnailName = getFileThumName(path);
        if (TextUtils.isEmpty(thumbnailName)) {
            return null;
        } else {
            return new File(getImageThumPath(), thumbnailName).getAbsolutePath();
        }
    }

    /**
     * 获取文件夹大小
     *
     * @param file File实例
     * @return long
     */
    public static long getFolderSize(java.io.File file) {

        long size = 0;
        try {
            java.io.File[] fileList = file.listFiles();
            for (File aFileList : fileList) {
                if (aFileList.isDirectory()) {
                    size = size + getFolderSize(aFileList);
                } else {
                    size = size + aFileList.length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 格式化单位
     */
    public static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }

    /**
     * 删除指定目录下文件及目录
     *
     * @param filePath       要删除的文件路径
     * @param deleteThisPath 是否删除目录
     */
    public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                File file = new File(filePath);
                if (file.isDirectory()) {// 处理目录
                    File files[] = file.listFiles();
                    for (File file1 : files) {
                        deleteFolderFile(file1.getAbsolutePath(), deleteThisPath);
                    }
                }
                if (!file.isDirectory()) {// 如果是文件，删除
                    file.delete();
                } else {// 目录
                    if (deleteThisPath && file.listFiles().length == 0) {// 目录下没有文件或者目录，删除
                        file.delete();
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
